import { ref, Ref, unref,reactive } from "vue";
import { FormInstance, } from 'element-plus'
// 用于弹窗公共通用属性
export interface DialogGeneralProps<T = any, L = any> {
    showSelf: Ref<boolean>,
    tempInfo: Ref<T | undefined>,
    commitFormComponent: Ref<FormInstance | undefined>,
    fetchList: Ref<L[]>,
    loading: Ref<boolean>,
    handleClose: (cb?: () => Promise<any> | any) => void,
    show: (p?: T) => void
}
export default function <T = any, L = any>(): DialogGeneralProps<T, L> {
    const showSelf = ref(false),
        tempInfo = ref<T>(),
        commitFormComponent = ref<FormInstance>(),
        fetchList = ref<L[]>([]) as Ref<L[]>,
        loading = ref(false),
        handleClose = async (cb?: () => Promise<any> | any) => {
            try {
                if (cb) {
                    await cb()
                }
                unref(commitFormComponent)?.resetFields()
                tempInfo.value = undefined
                loading.value = false
                fetchList.value = []
                showSelf.value = false
                
            } catch (e) {
                console.error(e);
            }
        },
        show = (p?: T) => {
            tempInfo.value = p
            showSelf.value = true

        }
    return {
        showSelf,
        tempInfo,
        handleClose,
        commitFormComponent,
        fetchList,
        loading,
        show
    }
}